import os
import array

def getSize(l):
    m = 0
    for x in l:
        m = m | x;
    for s in range(9):
        if (m & (1 << s)):
            break
    return (8 - s) + 1          
    
if __name__ == "__main__":
    print "<i> Convertendo formato de fontes da pasta atual"
    flist = [f for f  in os.listdir(os.getcwd()) if (f[-3:] == "bin") and (f[:5] == "[raw]")]
    ###
    for item in flist:
        ###
        size = os.path.getsize(item)
        print "\tNome:\t\t", item, "\n\tTamanho:\t", size, " bytes"
        ###
        binFile = open(item,"rb")
        data = [ord(n) for n in binFile.read(size)]
        binFile.close()
        #temp = open("temp.bin","wb")
        #temp.write("".join([chr(c) for c in data]))
        #temp.close()
        print "<i> Convertendo dados..."

        ###
        x = 0
        c = 0;
        tmp = open(item[5:],"wb")
        
        while( x < size):
            #salva 1 byte com o tamanho da letra
            tmp.write( chr(getSize(data[x:x+8])) )
            tmp.write("".join([chr(c) for c in data[x:x+8]]))
            #getSize(data[x:x+8])
            x = x + 8
            c = c + 1
        tmp.close()
